#include "localimageview.h"

#include <cstring>
#include <cstdio>
using namespace std;

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
using namespace cv;

#include <QDebug>

#include "timestamp.h"
LocalImageView::LocalImageView(QWidget *parent) :
    ImageView(parent)
{
    this->m_device = -1;
    this->m_source = "";
    this->m_last_timer_state = false;
    this->m_save_folder = "";
    QObject::connect(&this->m_timer, SIGNAL(timeout()), this, SLOT(grab()));

}

void LocalImageView::changeVideoSource(QString source){
    if(source != this->m_source){
        if(source.startsWith("video")){
            this->m_device = atoi(source.right(source.length() - strlen("video")).toStdString().c_str());
            this->m_source = source;
            this->m_timer.setInterval(30);
            this->m_capture.open(this->m_device);
            this->m_timer.start();
        }else if(source.startsWith("/")){//just for linux
            this->m_device = -1;
            this->m_source = source;
            this->m_timer.setInterval(30);
            this->m_capture.open(this->m_source.toStdString());
            this->m_timer.start();
        }else{
            this->m_device = -1;
            this->m_source = "";
            this->m_timer.stop();
        }
    }
}

void LocalImageView::stopGrab(){
    if(this->m_timer.isActive()){
        this->m_last_timer_state = true;
        this->m_timer.stop();
    }else{
        this->m_last_timer_state = false;
    }
}

void LocalImageView::continueGrab(){
    if(this->m_last_timer_state){
        this->m_timer.setInterval(30);
        this->m_timer.start();
        this->m_last_timer_state = false;
    }
}

void LocalImageView::grab(){
    if(m_img_mutex.tryLock()){
        this->m_capture >> this->m_image;
        if(!m_image.data){
            this->m_timer.stop();
            this->m_capture.release();
            return;
        }
        this->showImage(this->m_image);
        saveImg2Video(this->m_image);
        m_img_mutex.unlock();
    }
}

void LocalImageView::saveImg2Video(Mat img){
    this->m_save_mutex.lock();

    if(this->m_save_state){
        if(!this->m_save_writer.isOpened()){
            string filename = this->m_save_folder.toStdString() + "/" + TimeStamp::now()->toFileName() + ".avi";
            this->m_save_writer.open(filename,
                                     CV_FOURCC('X','V','I','D'),
                                     30,
                                     Size(this->m_image.cols, this->m_image.rows),
                                     true);
        }
        this->m_save_writer<<img;
    }
    this->m_save_mutex.unlock();
}

void LocalImageView::setSaveState(int s){
    this->m_save_mutex.lock();
    if(s > 0){
        this->m_save_state = true;
        this->startSaveVideo();
    }else{
        this->m_save_state = false;
        this->stopSaveVideo();
    }
    this->m_save_mutex.unlock();
}


void LocalImageView::startSaveVideo(){
    if(!this->m_image.empty()){
        string filename = TimeStamp::now()->toFileName() + ".avi";
        m_save_writer = VideoWriter(filename,
                                    CV_FOURCC('X','V','I','D'),
                                    30,
                                    Size(this->m_image.cols, this->m_image.rows),
                                    true);
    }else{
        qDebug()<<"no image now, wait the first image to record";
    }
}

void LocalImageView::stopSaveVideo(){
    if(this->m_save_writer.isOpened()){
        this->m_save_writer = VideoWriter();
    }
}


